﻿using System;
using System.Collections.Generic;
using System.Text;
using Pixysoft.Framework.Reflection;
using Pixysoft.Weblications.Sango.Dao;
using Pixysoft.Weblications.Sango.Formular;
using Pixysoft.Weblications.Sango.Constant;
using Pixysoft.Weblications.Sango.Environment;
using Pixysoft.Weblications.Sango.Protocol.player;
using Pixysoft.Weblications.Sango.Protocol;

namespace Pixysoft.Weblications.Sango.Command.party
{
    partial class SangoPartyHandler
    {
        public party_missiondonate party_missiondonate(long missiontypecode, long donatevalue)
        {
            string userCode = SangoManager.Authentication();

            party_missiondonate response = ReflectionManager.CreatePojo<party_missiondonate>();
            response.Succeed = false;

            Entities.Usr_Partymember member = PartyDao.tryGetActiveMember(userCode);

            if (member == null)
            {
                response.Message = "尚未加入军团";
                return response;
            }

            Entities.Usr_Party party = PartyDao.tryGetActiveParty(member.Partyprimarycode);

            Entities.Usr_Partymission mission = PartyDao.getMission(party.Primarycode, missiontypecode);

            Entities.Usr_Profile profile = UsrProfileDao.getUsrProfile(userCode);

            Entities.Usr_Activity history = UsrActivityDao.getHistory(userCode);

            Entities.Usr_Property property = UsrPropertyDao.getProperty(userCode);


            long maxLevel = PartyDao.getMissionMaxLevel(party, mission.Missiontypecode);

            long startLevel = mission.Missionlevel;

            long expvalue = PartyFormular.getDonateExperienceValue(donatevalue);//1:1

            if (!PartyFormular.canMissionDonate(history, expvalue, profile.Userlevel))
            {
                response.Message = "今日捐献已经满";
                return response;
            }

            if (!UsrProfileDao.tryDecreaseCashvalue(profile, expvalue))
            {
                response.Message = "银币余额不足";
                return response;
            }

            if (mission.Missionlevel >= maxLevel)
            {
                response.Message = "军团科技已经达到最大等级";
                return response;
            }

            IEntityNoebeTransaction command = SettingHelper.EntityTransaction;

            command.Begin(delegate()
            {

                history.Partymissiondonatevalue = (history.Partymissiondonatevalue + expvalue);
                command.Update(history);

                // mission

                PartyFormular.upgradeMission(mission, expvalue, maxLevel);
                command.Update(mission);

                // contribute

                property.Partycontributionvalue = (property.Partycontributionvalue + expvalue);
                command.Update(property);

                // update party && open new mission

                if (mission.Missiontypecode == PtyMissionTypecodeCollection.LEVELUP)
                {
                    if (startLevel < mission.Missionlevel)// 出现了商会等级升级
                    {
                        party.Maxmembercount = (party.Maxmembercount + PartyFormular.default_membercount_levelup * (mission.Missionlevel - startLevel));
                    }

                    party.Partylevel = (mission.Missionlevel);

                    // 搜索商会等级升级后，开放的使命 由于可能存在连续升级 因此需要使用循环

                    for (long level = startLevel + 1; level <= mission.Missionlevel; level++)
                    {
                        foreach (Entities.Pty_Partymission sys in PtyPartymissionHelper.searchOpenMissions(level))
                        {
                            Entities.Usr_Partymission orm = PartyDao.createMission(party, sys.Missiontypecode);
                            command.Insert(orm);
                        }
                    }
                }

                command.Update(party);

                command.Commit();
            });


            response.profile = UsrProfileFacade.ToProfile(profile);
            response.activity = UsrProfileFacade.ToActivity(profile, history);
            response.property = UsrProfileFacade.ToProperty(property);
            response.Succeed = true;
            return response;
        }


    }
    public interface party_missiondonate : ISangoResponse
    {
        long current { get;set;}
        long total { get;set;}

        player_profile profile { get;set;}
        player_activity activity { get;set;}
        player_property property { get;set;}
    }
}
